﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ClientSatisfactionBaseOnServiceStation : System.Web.UI.Page
    {
        public Int32 TotalNumber { get; set; }

        public Int32 validNumberMaintainQuality { get; set; }
        public Int32 validNumberMaintainPeriod { get; set; }
        public Int32 validNumberServiceRaction { get; set; }
        public Int32 validNumberProductQuality { get; set; }
        public Int32 validNumberproductFunction { get; set; }
        public Int32 validNumberDistributeService { get; set; }
        public Int32 validNumberCost { get; set; }
        public Int32 validNumberWebSite { get; set; }

        public Int32 validMarkMaintainQuality { get; set; }
        public Int32 validMarkMaintainPeriod { get; set; }
        public Int32 validMarkServiceRaction { get; set; }
        public Int32 validMarkProductQuality { get; set; }
        public Int32 validMarkproductFunction { get; set; }
        public Int32 validMarkDistributeService { get; set; }
        public Int32 validMarkCost { get; set; }
        public Int32 validMarkWebSite { get; set; }

        public Int32 totalMarkMaintainQuality { get; set; }
        public Int32 totalMarkMaintainPeriod { get; set; }
        public Int32 totalMarkServiceRaction { get; set; }
        public Int32 totalMarkProductQuality { get; set; }
        public Int32 totalMarkproductFunction { get; set; }
        public Int32 totalMarkDistributeService { get; set; }
        public Int32 totalMarkCost { get; set; }
        public Int32 totalMarkWebSite { get; set; }

        public Int32 satisfactionMaintainQuality { get; set; }
        public Int32 satisfactionMaintainPeriod { get; set; }
        public Int32 satisfactionServiceRaction { get; set; }
        public Int32 satisfactionProductQuality { get; set; }
        public Int32 satisfactionproductFunction { get; set; }
        public Int32 satisfactionDistributeService { get; set; }
        public Int32 satisfactionCost { get; set; }
        public Int32 satisfactionWebSite { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2010; i <= DateTime.Now.Year; i++)
                {
                    yearDropDownList.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                ServiceStationService ssService = new ServiceStationService();
                quarterListBox.DataSource = ssService.GetALLServiceStations();
                quarterListBox.DataTextField = "Name";
                quarterListBox.DataValueField = "ID";
                quarterListBox.DataBind();
                quarterListBox.SelectedValue = "1";
                BindPage();
            }
        }

        private void BindPage()
        {
            FeedBackService feedService = new FeedBackService();
            List<Feedback> feedbackList = feedService.GetFeedBackByYearAndServiceStation(Convert.ToInt32(yearDropDownList.SelectedValue), Convert.ToInt32(quarterListBox.SelectedValue)).ToList();
            TotalNumber = feedbackList.Count;

            validNumberCost = feedbackList.Where(f => !string.IsNullOrEmpty(f.Cost)).Count();
            validNumberDistributeService = feedbackList.Where(f => !string.IsNullOrEmpty(f.DistributeService)).Count();
            validNumberMaintainPeriod = feedbackList.Where(f => !string.IsNullOrEmpty(f.MaintainPeriod)).Count();
            validNumberMaintainQuality = feedbackList.Where(f => !string.IsNullOrEmpty(f.MaintainQuality)).Count();
            validNumberproductFunction = feedbackList.Where(f => !string.IsNullOrEmpty(f.ProductFunction)).Count();
            validNumberProductQuality = feedbackList.Where(f => !string.IsNullOrEmpty(f.ProductQuality)).Count();
            validNumberServiceRaction = feedbackList.Where(f => !string.IsNullOrEmpty(f.ServiceResponse)).Count();
            validNumberWebSite = feedbackList.Where(f => !string.IsNullOrEmpty(f.WebsiteEvaluate)).Count();

            foreach (Feedback fb in feedbackList)
            {
                if (!String.IsNullOrEmpty(fb.WebsiteEvaluate))
                {
                    validMarkWebSite += Convert.ToInt32(fb.WebsiteEvaluate);
                }
                if (!String.IsNullOrEmpty(fb.ServiceResponse))
                {
                    validMarkServiceRaction += Convert.ToInt32(fb.ServiceResponse);
                }
                if (!String.IsNullOrEmpty(fb.ProductQuality))
                {
                    validMarkProductQuality += Convert.ToInt32(fb.ProductQuality);
                }
                if (!String.IsNullOrEmpty(fb.ProductFunction))
                {
                    validMarkproductFunction += Convert.ToInt32(fb.ProductFunction);
                }
                if (!String.IsNullOrEmpty(fb.MaintainQuality))
                {
                    validMarkMaintainQuality += Convert.ToInt32(fb.MaintainQuality);
                }
                if (!String.IsNullOrEmpty(fb.MaintainPeriod))
                {
                    validMarkMaintainPeriod += Convert.ToInt32(fb.MaintainPeriod);
                }
                if (!String.IsNullOrEmpty(fb.DistributeService))
                {
                    validMarkDistributeService += Convert.ToInt32(fb.DistributeService);
                }
                if (!String.IsNullOrEmpty(fb.Cost))
                {
                    validMarkCost += Convert.ToInt32(fb.Cost);
                }
            }

            totalMarkCost = validMarkCost + 5 * validNumberCost;
            totalMarkDistributeService = validMarkDistributeService + 5 * validNumberDistributeService;
            totalMarkMaintainPeriod = validMarkMaintainPeriod + 5 * validNumberMaintainPeriod;
            totalMarkMaintainQuality = validMarkMaintainQuality + 5 * validNumberMaintainQuality;
            totalMarkproductFunction = validMarkproductFunction + 5 * validNumberproductFunction;
            totalMarkProductQuality = validMarkProductQuality + 5 * validNumberProductQuality;
            totalMarkServiceRaction = validMarkServiceRaction + 5 * validNumberServiceRaction;
            totalMarkWebSite = validMarkWebSite + 5 * validNumberWebSite;

            satisfactionMaintainQuality = validNumberMaintainQuality == 0 ? 0 : totalMarkMaintainQuality / validNumberMaintainQuality;
            satisfactionMaintainPeriod = validNumberMaintainPeriod == 0 ? 0 : totalMarkMaintainPeriod / validNumberMaintainPeriod;
            satisfactionServiceRaction = validNumberServiceRaction == 0 ? 0 : totalMarkServiceRaction / validNumberServiceRaction;
            satisfactionProductQuality = validNumberProductQuality == 0 ? 0 : totalMarkMaintainQuality / validNumberProductQuality;
            satisfactionproductFunction = validNumberproductFunction == 0 ? 0 : totalMarkproductFunction / validNumberproductFunction;
            satisfactionDistributeService = validNumberDistributeService == 0 ? 0 : totalMarkDistributeService / validNumberDistributeService;
            satisfactionCost = validNumberCost == 0 ? 0 : totalMarkCost / validNumberCost;
            satisfactionWebSite = validNumberWebSite == 0 ? 0 : totalMarkWebSite / validNumberWebSite;

            averageLabel.Text = ((satisfactionMaintainQuality + satisfactionMaintainPeriod + satisfactionServiceRaction + satisfactionProductQuality + satisfactionproductFunction + satisfactionDistributeService + satisfactionCost + satisfactionWebSite) / 8).ToString();
            Image1.ImageUrl = GetChartURL(satisfactionMaintainQuality);
            Image2.ImageUrl = GetChartURL(satisfactionMaintainPeriod);
            Image3.ImageUrl = GetChartURL(satisfactionServiceRaction);
            Image4.ImageUrl = GetChartURL(satisfactionProductQuality);
            Image5.ImageUrl = GetChartURL(satisfactionproductFunction);
            Image6.ImageUrl = GetChartURL(satisfactionDistributeService);
            Image7.ImageUrl = GetChartURL(satisfactionCost);
            Image8.ImageUrl = GetChartURL(satisfactionWebSite);
        }

        string GetChartURL(int a)
        {
            string url = "http://chart.apis.google.com/chart?cht=bhs&chco=4d89f9,c6d9fd&chd=t:{0}|100&chs=100x25";

            return string.Format(url, a * 10);
        }

        protected void quarterListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPage();
        }

        protected void yearDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPage();
        }
    }
}
